package server;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;

import vo.*;

public class ChatServer {
	
	public void sendToAllClients(String sender, String str) throws IOException {
		synchronized(ServerService.CONNECTION_LIST) {
			
			SystemMessageVO msg = new SystemMessageVO(SystemMessageVO.CHAT_MESSAGE, sender + " : " + str);
			
			Enumeration<Connection> en = Collections.enumeration(ServerService.CONNECTION_LIST);
			while(en.hasMoreElements()) {
				Connection conn = en.nextElement();
				if(!conn.thisThread.getState().equals("TERMINATED")
						&& conn.player.getState().equals("ACTIVE"))
					conn.write(msg);
//				System.out.println(conn.player.getState());
			}
		}
	}
	
	public void sendToAllClients(String str) throws IOException {
		synchronized(ServerService.CONNECTION_LIST) {
			
			SystemMessageVO msg = new SystemMessageVO(SystemMessageVO.NORMAL_MESSAGE, str);
			
			Enumeration<Connection> en = Collections.enumeration(ServerService.CONNECTION_LIST);
			while(en.hasMoreElements()) {
				Connection conn = en.nextElement();
				if(!conn.thisThread.getState().equals("TERMINATED")
						&& conn.player.getState().equals("ACTIVE"))
					conn.write(msg);
//				System.out.println(conn.player.getState());
			}
		}
	}
	
}
